<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gallery extends AppController {

	public function __construct(){
		parent::__construct();
		$this->load->model('gallery_model');
	}

	public function admin_index(){
		$pag  = array(
			'table' => 'image',
			'uri_segment' => 4,
			'sort' => false,
			'per_page' =>12,
			'base_url' => '/admin/gallery/index'
		);
		
		$pag = $this->paginate($pag);
		$pag['total_rows'] =  $this->getEM()->getRepository('\Entity\ShImage')->total_rows($pag);
		$this->pagination->initialize($pag);
		$this->data['pagination_link'] = $this->pagination->create_links();
		$this->data['pagination'] = $this->getEM()->getRepository('\Entity\ShImage')->backendPaginate($pag);

		$this->data['breadcrumb'] = array(
			'Hình ảnh' => null,
			'ext' => array(
				'button' => array('name'=>'Thêm hình ảnh','url'=>'gallery/upload')
			)
		);
		$this->isAjax();
		$this->render('gallery/admin_index');
	}
	public function mod_index(){
		$pag  = array(
			'table' => 'image',
			'uri_segment' => 4,
			'sort' => false,
			'per_page' =>12,
			'base_url' => '/mod/gallery/index'
		);
		
		$pag = $this->paginate($pag);
		$pag['total_rows'] =  $this->getEM()->getRepository('\Entity\ShImage')->total_rows($pag);
		$this->pagination->initialize($pag);
		$this->data['pagination_link'] = $this->pagination->create_links();
		$this->data['pagination'] = $this->getEM()->getRepository('\Entity\ShImage')->backendPaginate($pag);

		$this->data['breadcrumb'] = array(
			'Hình ảnh' => null,
			'ext' => array(
				'button' => array('name'=>'Thêm hình ảnh','url'=>'gallery/upload')
			)
		);
		$this->isAjax();
		$this->render('gallery/mod_index');
	}

	public function admin_upload(){
		if($this->input->post()):
			$this->gallery_model->single_upload($this->getEM());
		endif;

		$this->layout = 'layout/back';
		$this->data['breadcrumb'] = array(
			'Hình ảnh' => base_url().'admin/gallery',
			'Upload' => null
		);
		$this->render('gallery/admin_uploadify',array('uploadify/jquery.uploadify.min'),array('uploadify'));
	}
	public function mod_upload(){
		if($this->input->post()):
			$this->gallery_model->single_upload($this->getEM());
		endif;

		$this->layout = 'layout/back';
		$this->data['breadcrumb'] = array(
			'Hình ảnh' => base_url().'mod/gallery',
			'Upload' => null
		);
		$this->render('gallery/mod_uploadify',array('uploadify/jquery.uploadify.min'),array('uploadify'));
	}
	public function admin_uploadAjax(){
		if(!empty($_FILES['userfile']))
			$this->gallery_model->single_upload($this->getEM(),true);
		else
			echo json_encode(array(
				'status'=>'error','msg'=>'Không tìm thấy file cần upload'
			));
	}
	public function mod_uploadAjax(){
		if(!empty($_FILES['userfile']))
			$this->gallery_model->single_upload($this->getEM(),true);
		else
			echo json_encode(array(
				'status'=>'error','msg'=>'Không tìm thấy file cần upload'
			));
	}
	public function admin_delete(){
		if(!$this->input->post() || !$this->input->post('img'))
			redirect('/admin/gallery');

		if($this->gallery_model->delete())
			$this->session->set_flashdata('success','Xóa hình thành công');
		else
			$this->session->set_flashdata('error','Xóa hình thất bại, thử lại');

		redirect('/admin/gallery');
	}
	public function mod_delete(){
		if(!$this->input->post() || !$this->input->post('img'))
			redirect('/mod/gallery');

		if($this->gallery_model->delete())
			$this->session->set_flashdata('success','Xóa hình thành công');
		else
			$this->session->set_flashdata('error','Xóa hình thất bại, thử lại');

		redirect('/mod/gallery');
	}
}